package net.office.controller;

import net.office.bean.BaseDict;
import net.office.bean.StaffInfo;
import net.office.bean.StaffLeave;
import net.office.service.BaseDictService;
import net.office.service.LoginService;
import net.office.service.StaffLeaveService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StaffLeaveController {

    @Autowired
    private StaffLeaveService staffLeaveService;

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private LoginService loginService;

    /*查询个人列表*/
    @RequestMapping("/leave/person/list.do")
    public String personList(HttpSession session, Model model){
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");

        List<StaffLeave> staffLeaveList = staffLeaveService.findAllByStaffId(staffInfo.getStaffId());

        for(int i=0; i<staffLeaveList.size(); i++){
            if(staffLeaveList.get(i).getStaffLeaveApproveResult() != null) {
                staffLeaveList.get(i).setApproveResultDict(baseDictService.findOneByDictId(staffLeaveList.get(i).getStaffLeaveApproveResult()));
            }
        }
        model.addAttribute("staffLeaveList", staffLeaveList);

        List<BaseDict> approveResultList = baseDictService.findByDictTypeCode(TemplateParam.LEAVE_APPROVE_RESULT_CODE);
        model.addAttribute("approveResultList", approveResultList);

        return "holiday";
    }

    /*搜索个人列表*/
    @RequestMapping("/leave/person/search.do")
    public String personSearch(@RequestParam(value = "staffNo", required = false) String staffNo,
                               @RequestParam(value = "staffName", required = false) String staffName,
                               @RequestParam(value = "staffLeaveStartDate", required = false) Date staffLeaveStartDate,
                               @RequestParam(value = "staffLeaveEndDate", required = false) Date staffLeaveEndDate,
                               @RequestParam(value = "staffLeaveApproveResult", required = false) Integer staffLeaveApproveResult,
                               HttpSession session, Model model){

        StaffLeave staffLeave = new StaffLeave();
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        staffLeave.setStaffId(staffInfo.getStaffId());

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffLeave.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffLeave.setStaffName(staffName);
        }
        if(staffLeaveStartDate!=null){
            staffLeave.setStaffLeaveStartDate(staffLeaveStartDate);
        }
        if(staffLeaveEndDate!=null){
            staffLeave.setStaffLeaveEndDate(staffLeaveEndDate);
        }
        if(staffLeaveApproveResult!=null){
            staffLeave.setStaffLeaveApproveResult(staffLeaveApproveResult);
        }

        List<StaffLeave> staffLeaveList = staffLeaveService.findByStaffLeave(staffLeave);

        for(int i=0; i<staffLeaveList.size(); i++){
            if(staffLeaveList.get(i).getStaffLeaveApproveResult() != null) {
                staffLeaveList.get(i).setApproveResultDict(baseDictService.findOneByDictId(staffLeaveList.get(i).getStaffLeaveApproveResult()));
            }
        }

        model.addAttribute("staffLeaveList", staffLeaveList);

        List<BaseDict> approveResultList = baseDictService.findByDictTypeCode(TemplateParam.LEAVE_APPROVE_RESULT_CODE);
        model.addAttribute("approveResultList", approveResultList);

        return "holiday";
    }

    /*查询全部列表*/
    @RequestMapping("/leave/all/list.do")
    public String allList(Model model){
        List<StaffLeave> staffLeaveList = staffLeaveService.findAll();

        for(int i=0; i<staffLeaveList.size(); i++){
            if(staffLeaveList.get(i).getStaffLeaveApproveResult() != null) {
                staffLeaveList.get(i).setApproveResultDict(baseDictService.findOneByDictId(staffLeaveList.get(i).getStaffLeaveApproveResult()));
            }
        }

        model.addAttribute("staffLeaveList", staffLeaveList);

        List<BaseDict> approveResultList = baseDictService.findByDictTypeCode(TemplateParam.LEAVE_APPROVE_RESULT_CODE);
        model.addAttribute("approveResultList", approveResultList);

        return "holiday";
    }

    /*搜索全部列表*/
    @RequestMapping("/leave/all/search.do")
    public String allSearch(@RequestParam(value = "staffNo", required = false) String staffNo,
                            @RequestParam(value = "staffName", required = false) String staffName,
                            @RequestParam(value = "staffLeaveStartDate", required = false) Date staffLeaveStartDate,
                            @RequestParam(value = "staffLeaveEndDate", required = false) Date staffLeaveEndDate,
                            @RequestParam(value = "staffLeaveApproveResult", required = false) Integer staffLeaveApproveResult,
                            Model model){

        StaffLeave staffLeave = new StaffLeave();

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffLeave.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffLeave.setStaffName(staffName);
        }
        if(staffLeaveStartDate!=null){
            staffLeave.setStaffLeaveStartDate(staffLeaveStartDate);
        }
        if(staffLeaveEndDate!=null){
            staffLeave.setStaffLeaveEndDate(staffLeaveEndDate);
        }
        if(staffLeaveApproveResult!=null){
            staffLeave.setStaffLeaveApproveResult(staffLeaveApproveResult);
        }

        List<StaffLeave> staffLeaveList = staffLeaveService.findByStaffLeave(staffLeave);

        for(int i=0; i<staffLeaveList.size(); i++){
            if(staffLeaveList.get(i).getStaffLeaveApproveResult() != null) {
                staffLeaveList.get(i).setApproveResultDict(baseDictService.findOneByDictId(staffLeaveList.get(i).getStaffLeaveApproveResult()));
            }
        }

        model.addAttribute("staffLeaveList", staffLeaveList);

        List<BaseDict> approveResultList = baseDictService.findByDictTypeCode(TemplateParam.LEAVE_APPROVE_RESULT_CODE);
        model.addAttribute("approveResultList", approveResultList);

        return "holiday";
    }

    /*新增*/
    @RequestMapping("/leave/insert.do")
    public String insert(@RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffLeaveStartDate", required = false) Date staffLeaveStartDate,
                         @RequestParam(value = "staffLeaveEndDate", required = false) Date staffLeaveEndDate,
                         @RequestParam(value = "staffLeaveReason", required = false) String staffLeaveReason,
                         Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/leave/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/leave/toAdd.do";
        }

        StaffLeave staffLeave = new StaffLeave();
        staffLeave.setStaffId(staffInfo.getStaffId());
        staffLeave.setStaffLeaveSubmitTime(new Date());
        staffLeave.setStaffLeaveApproveResult(TemplateParam.LEAVE_APPROVE_RESULT_WAIT);

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffLeave.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffLeave.setStaffName(staffName);
        }
        if(staffLeaveStartDate!=null){
            staffLeave.setStaffLeaveStartDate(staffLeaveStartDate);
        }
        if(staffLeaveEndDate!=null){
            staffLeave.setStaffLeaveEndDate(staffLeaveEndDate);
        }
        if(staffLeaveReason!=null && staffLeaveReason.trim().length()!=0){
            staffLeave.setStaffLeaveReason(staffLeaveReason);
        }

        int res = staffLeaveService.insert(staffLeave);

        if(res > 0){
            if(true){
                return "redirect:/leave/all/list.do";
            }else{
                return "redirect:/leave/person/list.do";
            }
        }

        return "holiday-add";
    }

    /*修改*/
    @RequestMapping("/leave/update.do")
    public String update(@RequestParam(value = "staffLeaveId") Integer staffLeaveId,
                         @RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffLeaveStartDate", required = false) Date staffLeaveStartDate,
                         @RequestParam(value = "staffLeaveEndDate", required = false) Date staffLeaveEndDate,
                         @RequestParam(value = "staffLeaveReason", required = false) String staffLeaveReason,
                         Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/leave/toEdit.do?staffLeaveId="+staffLeaveId+"";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/leave/toEdit.do?staffLeaveId="+staffLeaveId+"";
        }

        StaffLeave staffLeave = new StaffLeave();
        staffLeave.setStaffLeaveId(staffLeaveId);
        staffLeave.setStaffId(staffInfo.getStaffId());
        staffLeave.setStaffLeaveSubmitTime(new Date());
        staffLeave.setStaffLeaveApproveResult(TemplateParam.LEAVE_APPROVE_RESULT_WAIT);

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffLeave.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffLeave.setStaffName(staffName);
        }
        if(staffLeaveStartDate!=null){
            staffLeave.setStaffLeaveStartDate(staffLeaveStartDate);
        }
        if(staffLeaveEndDate!=null){
            staffLeave.setStaffLeaveEndDate(staffLeaveEndDate);
        }
        if(staffLeaveReason!=null && staffLeaveReason.trim().length()!=0){
            staffLeave.setStaffLeaveReason(staffLeaveReason);
        }

        int res = staffLeaveService.updateByPrimaryKey(staffLeave);

        if(res > 0){

        }

        return "redirect:/leave/toEdit.do?staffLeaveId="+staffLeaveId+"";
    }

    /*修改（审批）*/
    @RequestMapping("/leave/approve/update.do")
    public String updateApprove(@RequestParam(value = "staffLeaveId") Integer staffLeaveId,
                @RequestParam(value = "staffLeaveApproveResult") Integer staffLeaveApproveResult,
                HttpSession session, Model model){

        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");

        StaffLeave staffLeave = new StaffLeave();
        staffLeave.setStaffLeaveApproveStaffId(staffInfo.getStaffId());
        staffLeave.setStaffLeaveApproveStaffNo(staffInfo.getStaffNo());
        staffLeave.setStaffLeaveApproveStaffName(staffInfo.getStaffName());
        staffLeave.setStaffLeaveApproveTime(new Date());

        staffLeave.setStaffLeaveId(staffLeaveId);
        staffLeave.setStaffLeaveApproveResult(staffLeaveApproveResult);

        int res = staffLeaveService.updateByPrimaryKeySelective(staffLeave);

        if(res > 0){

        }
        return "redirect:/leave/toEdit.do?staffLeaveId="+staffLeaveId+"";
    }

    /*删除单条(ajax)*/
    @RequestMapping("/leave/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("staffLeaveId") Integer staffLeaveId){

        int res = staffLeaveService.deleteByPrimaryKey(staffLeaveId);

        Map<String, String> map = new HashMap<String, String>();
        if(res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/leave/toAdd.do")
    public String toAdd(Model model){

        List<BaseDict> approveResultList = baseDictService.findByDictTypeCode(TemplateParam.LEAVE_APPROVE_RESULT_CODE);
        model.addAttribute("approveResultList", approveResultList);

        return "holiday-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/leave/toEdit.do")
    public String toEdit(@RequestParam("staffLeaveId") Integer staffTrainId, Model model){
        StaffLeave staffLeave = staffLeaveService.findOneByStaffLeaveId(staffTrainId);
        staffLeave.setApproveResultDict(baseDictService.findOneByDictId(staffLeave.getStaffLeaveApproveResult()));
        model.addAttribute("staffLeave", staffLeave);

        List<BaseDict> approveResultList = baseDictService.findByDictTypeCode(TemplateParam.LEAVE_APPROVE_RESULT_CODE);
        model.addAttribute("approveResultList", approveResultList);

        model.addAttribute("agreeCode", TemplateParam.LEAVE_APPROVE_RESULT_AGREE);
        model.addAttribute("disagreeCode", TemplateParam.LEAVE_APPROVE_RESULT_DISAGREE);

        return "holiday-edit";
    }

}
